<?php

namespace magein\laravel\seecms\command;

use Illuminate\Console\Command;
use Illuminate\Support\Str;

class Publish extends Command
{

    /**
     * 命令名称及签名
     *
     * @var string
     */
    protected $signature = 'seecms:publish {name?}';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = 'seecms publish';

    protected $help = '
    php artisan seecms:publish                            发布seecms
';

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $name = $this->argument('name');
        $name = $name ?: 'admin';
        if (!preg_match('/^[a-zA-Z]\w+/', $name)) {
            $this->error('目录名称错误');
            exit(1);
        }
        $name = Str::studly($name);

        $description = function ($type, $message) {
            $message = str_replace(app_path(), '', $message);
            $message = trim($message, '/');
            $message = trim($message, '\\');
            $this->info($type . ' : ' . $message);
        };

        $step = function ($model, $desc) {
            $this->info($model . ':' . $desc);
        };

        $continue = function ($message) use ($description) {
            $description('  --跳过', $message);
        };

        $progress = function ($message) use ($description) {
            $description('  --进度', $message);
        };

        $step('开始', '检查目录');

        /**
         * @param mixed ...$_
         * @return string
         */
        $app_dir = function (...$_) use ($name) {
            $dir = '';
            foreach ($_ as $item) {
                if (is_string($item)) {
                    $dir = '/' . trim($item, '/');
                }
            }
            return app_path($name . '/' . trim($dir, '/'));
        };

        $directories = [
            $app_dir('Controller'),
            $app_dir('Lang'),
            $app_dir('Page'),
        ];

        $origin_file = function ($name) {
            return __DIR__ . '/../../' . $name;
        };

        // 复制配置文件
        $config = config_path() . '/seecms.php';
        if (!is_file($config)) {
            copy($origin_file('config/seecms.php'), $config);
            $progress('创建config/seecms.php目录完成');
        } else {
            $progress('跳过config/seecms.php目录完成');
        }

        // 复制路由文件
        $config = base_path() . '/routes/seecms.php';
        if (!is_file($config)) {
            copy($origin_file('route/seecms.php'), $config);
            $progress('创建routes/seecms.php目录完成');
        } else {
            $progress('跳过routes/seecms.php目录完成');
        }

        $files = [
            $app_dir('lang') . '/en-us.php' => $origin_file('lang/en-us.php'),
            $app_dir('lang') . '/zh-cn.php' => $origin_file('lang/zh-cn.php'),
        ];

        foreach ($directories as $item) {
            if (!is_dir($item)) {
                mkdir($item, 0755, true);
                if (is_dir($item)) {
                    $progress('创建' . $item . '目录完成');
                }
            } else {
                $continue('创建' . $item);
            }
        }

        foreach ($files as $file => $origin) {
            if (!is_file($file)) {
                copy($origin, $file);
                if (is_file($item)) {
                    $progress('创建' . $item . '文件完成');
                }
            } else {
                $continue('创建' . $item);
            }
        }

        $db_host = env('db_host');
        if ($db_host) {
            $step('数据库', '创建表');
            $this->call('migrate');

            $step('数据库', '初始化数据');
            $this->call('seecms:seed');
        } else {
            $step('数据库', '数据库参数尚未配置');
        }

        $step('静态资源', '安装静态资源文件');
        $this->call('seecms:vendor');

        $this->line('');
        $this->line('******************************************************');
        $this->line('*******************laravel-seecms安装完成*****************');
        $this->line('******************************************************');
    }
}
